package com.sise.dubbo.vo;

import java.io.Serializable;

/**
 * web service 公共参数返回VO
 *
 * @author chl
 * @date 2016/6/2
 */
public class WsResponseMsgVO<T> extends WsResponseVO implements Serializable {

    private static final long serialVersionUID = -6940735963292571240L;

    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    /**
     * 构造成功响应数据.
     *
     * @param body
     * @return
     */
    public static <T> WsResponseMsgVO<T> builderSuccessRespData(T body) {
        return builderRespData(true, WsResultConstants.WS_CODE_SUCCESS, WsResultConstants.WS_MSG_SUCCESS, body);
    }

    /**
     * 构造断路器注解异常响应数据.
     *
     * @return
     */
    public static <T> WsResponseMsgVO<T> builderHystrixAnnotationErrRespData() {
        return builder(false, WsResultConstants.WS_CODE_HYSTRIX_ANNOTATION_ERR_BIZCODE, WsResultConstants.WS_MSG_HYSTRIX_ANNOTATIO_ERR_BIZCODE);
    }

    /**
     * 构造断路器降级响应数据.
     *
     * @return
     */
    public static <T> WsResponseMsgVO<T> builderHystrixFallBackRespData() {
        return builder(false, WsResultConstants.WS_CODE_HYSTRIX_FALL_BACK_BIZCODE, WsResultConstants.WS_MSG_HYSTRIX_FALL_BACK_BIZCODE);
    }

    /**
     * 构造相应数据
     *
     * @param code
     * @param msg
     * @param body
     */
    public static <T> WsResponseMsgVO<T> builderRespData(Boolean isSucceed, String code, String msg, T body) {
        WsResponseMsgVO<T> response = new WsResponseMsgVO<T>();
        response.setSucceed(isSucceed);
        response.setCode(code);
        response.setMsg(msg);
        if (body != null) {
            response.setData(body);
        }
        return response;
    }

    /**
     * 构造自定义响应应数据
     *
     * @param code
     * @param msg
     */
    public static <T> WsResponseMsgVO<T> builder(Boolean isSucceed, String code, String msg) {
        WsResponseMsgVO response = new WsResponseMsgVO();
        response.setSucceed(isSucceed);
        response.setCode(code);
        response.setMsg(msg);
        // 为防止IOS闪退
        response.setData("");
        return response;
    }

}
